package com.qg.rta.support.channel;

import cn.hutool.core.util.IdUtil;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.qg.rta.annnotions.ProductType;
import com.qg.rta.constant.ProductChannelConstant;
import com.qg.rta.dto.kjb.dto.RtaRequest;
import com.qg.rta.dto.request.RtaCommonRequest;
import com.qg.rta.modules.dto.RtaConfigDTO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;


@Component
@ProductType(ProductChannelConstant.KFY)
@Slf4j
public class KfyReport extends AbstractReport{
    @Override
    public boolean rtaReq(RtaCommonRequest rtaCommonRequest, RtaConfigDTO rtaConfigDTO) {
       log.info("请求快接包的请求参数位{},",JSON.toJSONString(rtaCommonRequest));
        RtaRequest.Device.Builder builder = RtaRequest.Device.newBuilder();
        if(StringUtils.isNotBlank(rtaCommonRequest.getImei()) && !rtaCommonRequest.getImei().contains("__")) {
            builder.setImei(rtaCommonRequest.getImei());
        }
        if(StringUtils.isNotBlank(rtaCommonRequest.getImeiSum()) && !rtaCommonRequest.getImeiSum().contains("__")) {
            builder.setImeiMd5(rtaCommonRequest.getImeiSum());
        }

        if(StringUtils.isNotBlank(rtaCommonRequest.getOaid()) && !rtaCommonRequest.getOaid().contains("__")) {
            builder.setOaid(rtaCommonRequest.getOaid());
        }

        if(StringUtils.isNotBlank(rtaCommonRequest.getOaidSum()) && !rtaCommonRequest.getOaidSum().contains("__")) {
            builder.setOaidMd5(rtaCommonRequest.getOaidSum());
        }

//        if(StringUtils.isNotBlank(rtaCommonRequest.getIdfa()) && !rtaCommonRequest.getIdfa().contains("__")) {
//            builder.setIdfa(rtaCommonRequest.getIdfa());
//        }
//        if(StringUtils.isNotBlank(rtaCommonRequest.getIdfaSum()) && !rtaCommonRequest.getIdfaSum().contains("__")) {
//            builder.setIdfaMd5(rtaCommonRequest.getIdfaSum());
//        }

        JSONObject jsonObject = JSONObject.parseObject(rtaConfigDTO.getProductConfig());
        String requestId = IdUtil.fastSimpleUUID();
        long ts = System.currentTimeMillis();
        String key=jsonObject.getString("key");
        RtaRequest rtaRequest = RtaRequest.newBuilder()
                .setRequestId(requestId)
                .setRequestTime(ts)
                .setChannel(jsonObject.getString("channel"))
                .setSign(SecureUtil.md5(requestId+ts+key))
                .setDevice(builder.build())
                .addPromotionType("kwai_lite_lahui").build();

        byte[] byteArray = rtaRequest.toByteArray();
        HttpRequest post = HttpUtil.createPost(rtaConfigDTO.getProductUrl());
        post.keepAlive(true);
        post.header("Content-Type", "application/x-protobuf");
        post.charset("UTF-8");
        HttpRequest body = post.body(byteArray);
        System.out.println(body.execute().body());
        String result = body.execute().body();
        JSONObject resultObject = JSONObject.parseObject(result);
        if(resultObject.containsKey("promotion_result")){
            JSONArray promotionResult = resultObject.getJSONArray("promotion_result");
            if(ObjectUtils.isNotEmpty(promotionResult)&&promotionResult.size()>0){
                JSONObject subJsonObject = JSONObject.parseObject(promotionResult.getString(0));
                if(!subJsonObject.containsKey("accept")){
                    return false;
                }
                return subJsonObject.getBoolean("accept");
            }
        }

        return false;
    }
}
